<template>
  <el-card class="blog-detail-card">
    <div slot="header">
      <h2>{{ blog.title }}</h2>
    </div>

    <div class="blog-meta">
      <span>作者: {{ blog.username }}</span>
      <span>发布时间: {{ blog.created_at }}</span>
      <span v-if="blog.updated_at !== blog.created_at">
        更新时间: {{ blog.updated_at }}
      </span>
    </div>

    <div class="blog-content">
      <template v-if="blog.content && blog.content.length > 0">
        <p v-for="(paragraph, index) in blog.content.split('\n')" :key="index">
          {{ paragraph }}
        </p>
      </template>
      <template v-else>
        <p>暂无内容</p>
      </template>
    </div>

    <div class="blog-actions" v-if="isOwner">
      <el-button
        @click="$router.push(`/blog/${blog.id}/edit`)"
        icon="el-icon-edit"
        >编辑
      </el-button>
      <el-button type="danger" @click="handleDelete" icon="el-icon-delete"
        >删除
      </el-button>
      <el-button @click="$router.push('/blogs')" icon="el-icon-back"
        >返回列表
      </el-button>
    </div>
  </el-card>
</template>

<script>
  export default {
    data() {
      return {
        blog: {},
        // isOwner: false,
      };
    },
    computed: {
      isAdmin() {
        return this.$store.getters.isAdmin;
      },
      user() {
        return this.$store.getters.user;
      },
      isOwner() {
        return this.user.userId === this.blog.user_id;
      },
    },
    created() {
      this.loadBlogDetail();
    },
    methods: {
      async loadBlogDetail() {
        try {
          const blogId = this.$route.params.id;
          const blog = await this.$axios.get(`/blog/${blogId}`);
          this.blog = blog.data;

          // 检查是否为作者
          // const user = JSON.parse(localStorage.getItem("user") || "{}");
          // this.isOwner = user.userId === blog.data.user_id;
        } catch (error) {
          this.$message.error("加载博客详情失败");
          console.error(error);
        }
      },
      async handleDelete() {
        this.$confirm("确定要删除这篇博客吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            try {
              await this.$axios.delete(`/blog/${this.blog.id}`);
              this.$message.success("删除成功");
              this.$router.push("/blogs");
            } catch (error) {
              this.$message.error("删除失败");
              console.error(error);
            }
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      },
    },
  };
</script>

<style scoped>
  .blog-detail-card {
    max-width: 1000px;
    margin: 20px auto;
  }

  .blog-meta {
    color: #666;
    font-size: 14px;
    margin: 10px 0 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
  }

  .blog-meta span {
    margin-right: 20px;
  }

  .blog-content {
    line-height: 1.8;
    color: #333;
    margin-bottom: 30px;
  }
  .dark .blog-content {
    color: #eee;
  }

  .blog-content p {
    margin-bottom: 15px;
  }

  .blog-actions {
    margin-top: 20px;
    text-align: right;
  }

  .blog-actions button {
    margin-left: 10px;
  }
</style>
